home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
DEMON
/
RISCOS2
/
TCP_131S.ARC
/
c
/
resolve_0
< prev
next >
Wrap
Text File
|
1993-12-22
|
16KB
|
680 lines
/*
* Copyright (c) 1985 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)res_comp.c 6.18 (Berkeley) 6/27/90";
#endif /* LIBC_SCCS and not lint */
#include "netdb.h"
#include "resolve.h"
#include <stdlib.h>
#include <stdio.h>
#include "arpa/nameser.h"
#include "netinet/in.h"
#include "socket.h"
#include "time.h"
static dn_find();
/*
* Expand compressed domain name 'comp_dn' to full domain name.
* 'msg' is a pointer to the begining of the message,
* 'eomorig' points to the first location after the message,
* 'exp_dn' is a pointer to a buffer of size 'length' for the result.
* Return size of compressed name or -1 if there was an error.
*/
dn_expand(msg, eomorig, comp_dn, exp_dn, length)
u_char *msg, *eomorig, *comp_dn, *exp_dn;
int length;
{
register u_char *cp, *dn;
register int n, c;
u_char *eom;
int len = -1, checked = 0;
dn = exp_dn;
cp = comp_dn;
eom = exp_dn + length;
/*
* fetch next label in domain name
*/
while (n = *cp++) {
/*
* Check for indirection
*/
switch (n & INDIR_MASK) {
case 0:
if (dn != exp_dn) {
if (dn >= eom)
return (-1);
*dn++ = '.';
}
if (dn+n >= eom)
return (-1);
checked += n + 1;
while (--n >= 0) {
if ((c = *cp++) == '.') {
if (dn + n + 2 >= eom)
return (-1);
*dn++ = '\\';
}
*dn++ = c;
if (cp >= eomorig) /* out of range */
return(-1);
}
break;
case INDIR_MASK:
if (len < 0)
len = cp - comp_dn + 1;
cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
if (cp < msg || cp >= eomorig) /* out of range */
return(-1);
checked += 2;
/*
* Check for loops in the compressed name;
* if we've looked at the whole message,
* there must be a loop.
*/
if (checked >= eomorig - msg)
return (-1);
break;
default:
return (-1); /* flag error */
}
}
*dn = '\0';
if (len < 0)
len = cp - comp_dn;
return (len);
}
/*
* Compress domain name 'exp_dn' into 'comp_dn'.
* Return the size of the compressed name or -1.
* 'length' is the size of the array pointed to by 'comp_dn'.
* 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
* is a pointer to the beginning of the message. The list ends with NULL.
* 'lastdnptr' is a pointer to the end of the arrary pointed to
* by 'dnptrs'. Side effect is to update the list of pointers for
* labels inserted into the message as we compress the name.
* If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
* is NULL, we don't update the list.
*/
dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
u_char *exp_dn, *comp_dn;
int length;
u_char **dnptrs, **lastdnptr;
{
register u_char *cp, *dn;
register int c, l;
u_char **cpp, **lpp, *sp, *eob;
u_char *msg;
dn = exp_dn;
cp = comp_dn;
eob = cp + length;
if (dnptrs != NULL) {
if ((msg = *dnptrs++) != NULL) {
for (cpp = dnptrs; *cpp != NULL; cpp++)
;
lpp = cpp; /* end of list to search */
}
} else
msg = NULL;
for (c = *dn++; c != '\0'; ) {
/* look to see if we can use pointers */
if (msg != NULL) {
if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
if (cp+1 >= eob)
return (-1);
*cp++ = (l >> 8) | INDIR_MASK;
*cp++ = l % 256;
return (cp - comp_dn);
}
/* not found, save it */
if (lastdnptr != NULL && cpp < lastdnptr-1) {
*cpp++ = cp;
*cpp = NULL;
}
}
sp = cp++; /* save ptr to length byte */
do {
if (c == '.') {
c = *dn++;
break;
}
if (c == '\\') {
if ((c = *dn++) == '\0')
break;
}
if (cp >= eob) {
if (msg != NULL)
*lpp = NULL;
return (-1);
}
*cp++ = c;
} while ((c = *dn++) != '\0');
/* catch trailing '.'s but not '..' */
if ((l = cp - sp - 1) == 0 && c == '\0') {
cp--;
break;
}
if (l <= 0 || l > MAXLABEL) {
if (msg != NULL)
*lpp = NULL;
return (-1);
}
*sp = l;
}
if (cp >= eob) {
if (msg != NULL)
*lpp = NULL;
return (-1);
}
*cp++ = '\0';
return (cp - comp_dn);
}
/*
* Skip over a compressed domain name. Return the size or -1.
*/
dn_skipname(comp_dn, eom)
u_char *comp_dn, *eom;
{
register u_char *cp;
register int n;
cp = comp_dn;
while (cp < eom && (n = *cp++)) {
/*
* check for indirection
*/
switch (n & INDIR_MASK) {
case 0: /* normal case, n == len */
cp += n;
continue;
default: /* illegal type */
return (-1);
case INDIR_MASK: /* indirection */
cp++;
}
break;
}
return (cp - comp_dn);
}
/*
* Search for expanded name from a list of previously compressed names.
* Return the offset from msg if found or -1.
* dnptrs is the pointer to the first name on the list,
* not the pointer to the start of the message.
*/
static
dn_find(exp_dn, msg, dnptrs, lastdnptr)
u_char *exp_dn, *msg;
u_char **dnptrs, **lastdnptr;
{
register u_char *dn, *cp, **cpp;
register int n;
u_char *sp;
for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
dn = exp_dn;
sp = cp = *cpp;
while (n = *cp++) {
/*
* check for indirection
*/
switch (n & INDIR_MASK) {
case 0: /* normal case, n == len */
while (--n >= 0) {
if (*dn == '.')
goto next;
if (*dn == '\\')
dn++;
if (*dn++ != *cp++)
goto next;
}
if ((n = *dn++) == '\0' && *cp == '\0')
return (sp - msg);
if (n == '.')
continue;
goto next;
default: /* illegal type */
return (-1);
case INDIR_MASK: /* indirection */
cp = msg + (((n & 0x3f) << 8) | *cp);
}
}
if (*dn == '\0')
return (sp - msg);
next: ;
}
return (-1);
}
/*
* Routines to insert/extract short/long's. Must account for byte
* order and non-alignment problems. This code at least has the
* advantage of being portable.
*
* used by sendmail.
*/
u_short
_getshort(msgp)
u_char *msgp;
{
register u_char *p = (u_char *) msgp;
#ifdef vax
/*
* vax compiler doesn't put shorts in registers
*/
register u_long u;
#else
register u_short u;
#endif
u = *p++ << 8;
return ((u_short)(u | *p));
}
u_long
_getlong(msgp)
u_char *msgp;
{
register u_char *p = (u_char *) msgp;
register u_long u;
u = *p++; u <<= 8;
u |= *p++; u <<= 8;
u |= *p++; u <<= 8;
return (u | *p);
}
putshort(s, msgp)
register u_short s;
register u_char *msgp;
{
msgp[1] = s;
msgp[0] = s >> 8;
}
putlong(l, msgp)
register u_long l;
register u_char *msgp;
{
msgp[3] = l;
msgp[2] = (l >>= 8);
msgp[1] = (l >>= 8);
msgp[0] = l >> 8;
}
static struct cache_s {
void *key;
int type;
int len;
struct hostent *cont;
time_t expires;
time_t accessed;
} cache[CACHE_SIZE];
static void kill_cache( struct cache_s *entry )
{
if( ! entry->key ) return;
free( entry->key );
entry->cont = NULL;
entry->key = NULL;
}
static int he_size( struct hostent *he )
{
int size;
char **cp;
struct mx_data **mx;
if( !he ) return 0;
size = sizeof( struct hostent ) + 2*sizeof(char *) + ((strlen(he->h_name)+4)&~3) + sizeof(struct mx_data *);
for( cp=he->h_aliases; *cp; cp++ ) size += sizeof(char *) + strlen(*cp)+1;
for( cp=he->h_addr_list; *cp; cp++ ) size += sizeof(char *) + he->h_length;
for( mx=he->h_mx; *mx; mx++ ) size += sizeof(struct mx_data) + strlen((*mx)->host) + sizeof(struct mx_data *) + 1;
return size;
}
static struct hostent *he_copy( char *mem, struct hostent *he )
{
struct hostent *new;
int i;
char **cp;
char **ds;
struct mx_data **mx,**xd;
if( !he ) return NULL;
new = (struct hostent *)mem;
memcpy( new, he, sizeof(*new) );
mem += sizeof(*new);
for( cp=he->h_aliases,i=1; *cp; cp++,i++ );
new->h_aliases = (char **)mem;
mem += i*sizeof(char *);
for( cp=he->h_addr_list,i=1; *cp; cp++,i++ );
new->h_addr_list = (char **)mem;
mem += i*sizeof(char *);
for( mx=he->h_mx,i=1; *mx; mx++,i++ );
new->h_mx = (struct mx_data **)mem;
mem += i*sizeof(struct mx_data *);
for( mx=new->h_mx; i>1; i--,mx++,mem+=sizeof(struct mx_data) ) *mx = (struct mx_data *)mem;
*mx = NULL;
strcpy( mem, he->h_name );
new->h_name = mem;
mem += ((strlen( he->h_name ) + 4)&~3);
for( ds=new->h_addr_list,cp=he->h_addr_list; *cp; cp++,ds++ ) {
*ds = mem;
memcpy( mem, *cp, he->h_length );
mem += he->h_length;
}
*ds = NULL;
for( ds=new->h_aliases,cp=he->h_aliases; *cp; cp++,ds++ ) {
*ds = mem;
i = strlen( *cp ) +1;
memcpy( mem, *cp, i );
mem += i;
}
*ds = NULL;
for( xd=new->h_mx,mx=he->h_mx; *mx; mx++,xd++ ) {
(*xd)->preference = (*mx)->preference;
(*xd)->host = (char *)mem;
i = strlen( (*mx)->host) + 1;
memcpy( mem, (*mx)->host, i );
mem += i;
}
*xd = NULL;
return new;
}
extern int cache_req( void *, int, int , struct hostent ** );
int cache_req( void *data, int type, int len, struct hostent **rval )
{
struct cache_s *find;
int i;
time_t t;
for( find=cache,i=CACHE_SIZE; i; i--,find++ )
if( (find->key) && (find->type==type) && (find->len==len) && ! memcmp( find->key, data, len ) ) {
time( &t );
if( difftime( t, find->expires ) >= 0 ) kill_cache( find );
else {
*rval = find->cont;
find->accessed = time(NULL);
return 0;
}
}
return -1;
}
void cache_it( void *data, int type, int len, struct hostent *result )
{
struct cache_s *overw=NULL, *clear=NULL;
int i;
time_t min;
char *mem;
time(&min);
for( overw=cache,i=CACHE_SIZE; i; i--,overw++ ) if( overw->key && difftime(min,overw->expires)<0 ) {
if( overw->type==type && overw->len==len && ! memcmp( overw->key, data, len ) ) break;
}
else clear = overw;
if( !i ) overw = clear;
if( ! overw ) for( overw=cache,clear=&cache[1],i=CACHE_SIZE-1,min=cache->accessed; i; i--,clear++ )
if( difftime( clear->accessed, min ) < 0 ) {
overw = clear;
min = clear->accessed;
}
mem = malloc( len+he_size(result)+3 );
if( ! mem ) return;
kill_cache( overw );
memcpy( mem, data, len );
overw->key = mem;
overw->type = type;
overw->len = len;
overw->accessed = time( NULL );
overw->expires = overw->accessed + CACHE_OBSOLETE;
len = (len+3)&~3;
overw->cont = he_copy( mem+len, result );
}
void cache_save( void )
{
FILE *f;
int i;
int n;
struct cache_s *cs;
char **cp;
struct mx_data **mx;
f = fopen( "<TCPIP$dir>.resolve.cache", "w" );
if( ! f ) return;
for( n=CACHE_SIZE,cs=cache; n; n--,cs++ ) if( cs->key ) {
switch( cs->type ) {
case CACHE_MX:
fprintf( f, "MX " );
goto keysave;
case CACHE_NAME:
fprintf( f, "NAME " );
keysave:
fwrite( cs->key, cs->len, 1, f );
fprintf( f, "\n" );
break;
}
fprintf( f, "%d %d\n", cs->expires, cs->accessed );
if( cs->cont ) {
for( mx=cs->cont->h_mx; *mx; mx++ ) {
fprintf( f, "%s", (*mx)->host );
if( mx[1] ) fputc( ' ', f );
}
fputc( '\n', f );
fprintf( f, "%d\n%s\n%d\n", cs->cont->h_length, cs->cont->h_name, cs->cont->h_addrtype );
for( cp=cs->cont->h_aliases; *cp; cp++ ) fprintf( f, "%s ", *cp );
fputc( '\n', f );
for( cp=cs->cont->h_addr_list; *cp; cp++ ) {
for( i=cs->cont->h_length; i; i-- ) fprintf( f, "%d.", (*(unsigned char **)cp)[i-1] );
fputc( ' ', f );
}
fputc( '\n', f );
}
else fprintf( f, "\n%d\n",
0 );
if( n>1 ) fputc( '\n', f );
}
fclose( f );
}
void cache_restore( void )
{
char buffer[500];
char hostname[256];
char addrs[100];
char aliases[500];
char mxhosts[500];
struct mx_data mxdata[30];
struct mx_data *mxptr[30];
struct mx_data **mxpp;
char key[256];
struct hostent he;
char *p_aliases[30];
char *p_addrs[35];
char *next;
char **at;
char *nextabyte;
FILE *f;
struct cache_s *c;
int i,j;
int n;
struct hostent *ohe;
f = fopen( "<TCPIP$dir>.resolve.cache", "r" );
if( ! f ) return;
cache_purge();
he.h_aliases = p_aliases;
he.h_addr_list = p_addrs;
he.h_mx = mxptr;
for( i=CACHE_SIZE,c=cache; i && ! feof(f) && ! ferror(f); i--,c++ ) {
c->cont = NULL;
fgets( buffer, 255, f );
if( ! memcmp( buffer, "NAME ", 5 ) ) {
c->type = CACHE_NAME;
memcpy( key, &buffer[5], strlen(buffer)-4 );
c->len = strlen( key );
if( key[c->len-1] == '\n' ) c->len--;
}
else if( ! memcmp( buffer, "MX ", 3 ) ) {
c->type = CACHE_MX;
memcpy( key, &buffer[3], strlen(buffer)-2 );
c->len = strlen( key );
if( key[c->len-1] == '\n' ) c->len--;
}
else break;
fgets( buffer, 255, f );
if( sscanf( buffer, "%d %d", &c->expires, &c->accessed ) < 2 ) break;
fgets( mxhosts, 499, f );
mxpp = mxptr;
for( j=0,next=mxhosts; *next != '\n' && *next; j++, mxpp++ ) {
while( *next == ' ' || *next == '\t' ) next++;
if( ! *next || *next == '\n' ) break;
*mxpp = &mxdata[j];
(*mxpp)->preference = j;
(*mxpp)->host = next;
while( *next != ' ' && *next && *next != '\t' && *next != '\n' ) next++;
if( *next == '\n' ) *next = 0;
else *(next++) = 0;
}
*mxpp = NULL;
fgets( buffer, 255, f );
if( sscanf( buffer, "%d", &he.h_length ) < 1 ) break;
if( he.h_length ) ohe = &he; else ohe = NULL;
if( ohe ) {
fgets( hostname, 255, f );
if( hostname[strlen(hostname)-1] == '\n' ) hostname[strlen(hostname)-1] = 0;
he.h_name = hostname;
fgets( buffer, 255, f );
if( sscanf( buffer, "%d", &he.h_addrtype ) < 1 ) break;
fgets( aliases, 499, f );
next = aliases;
for( n=29,at=he.h_aliases; n; at++,n-- ) {
while( *next==' ' || *next=='\t' ) next++;
if( *next=='\n' || *next==0 ) break;
*at = next;
while( *next != ' ' && *next && *next != '\t' && *next != '\n' ) next++;
*(next++) = 0;
}
*at = NULL;
fgets( buffer, 499, f );
for( nextabyte=addrs,next=buffer,n=100,at=he.h_addr_list; n; n--,at++,nextabyte+=he.h_length ) {
while( *next==' ' || *next=='\t' ) next++;
if( *next=='\n' || *next==0 ) break;
*at = nextabyte;
for( j=he.h_length; j; j-- ) {
nextabyte[j-1] = atoi(next);
while( *next!='.' && *next!=' ' && *next && *next!='\t' ) next++;
if( *next ) next++;
}
if( *next=='.' ) next++;
}
*at = NULL;
}
next = malloc( c->len + he_size( ohe ) + 3 );
memcpy( next, key, c->len );
c->key = next;
c->cont = he_copy( next + ((c->len+3)&~3), ohe );
fgets( buffer, 255, f );
}
fclose(f);
}
void cache_print( void )
{
struct cache_s *c;
int i;
char buf[255];
time_t t;
time( &t );
for( c=cache,i=CACHE_SIZE; i; i--,c++ ) if( c->key ) {
cwprintf(NULL, "type=" );
switch( c->type )
{
case CACHE_NAME:
case CACHE_MX:
memcpy( buf, c->key, c->len );
buf[c->len] = 0;
cwprintf(NULL, "%s, name=%s", c->type==CACHE_NAME?"NAME":"MX", buf );
break;
default:
cwprintf(NULL, "unknown" );
}
if( difftime( t, c->expires ) >= 0 )
cwprintf(NULL, ", expired since" );
else
cwprintf(NULL, ", expires at" );
cwprintf(NULL, " %s", ctime( &c->expires ) );
}
}
void cache_purge( void )
{
int i;
for( i=0; i<CACHE_SIZE; i++ ) kill_cache( &cache[i] );
}